package game.algorithms;

import models.Board;
import play.Logger;

import java.util.Random;

/**
 * Class.java is a class for...
 *
 * @author finni
 * @version 1.0
 *          Created 24.11.2011 15:00
 */
public class GameAlgo extends AbstractGameAlgorithms implements IGameAlgorithms
{
     /***
     * An algorithm used for deciding the computer game move
     * which is evaluated from the current game status stored in the board object
     * @param myBoard holds the session board object and info about the game being played
     * @return an integer which indicates the decided game move.
     */
    public int getComputerMove(Board myBoard)
    {
        int n = -1;

        // X X -
        // X X -
        // X X -
        if(myBoard.getMark(0) == 'X' && myBoard.getMark(1) == 'X' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }
        else if(myBoard.getMark(3) == 'X' && myBoard.getMark(4) == 'X' && myBoard.getMark(5) == '-')
        {
            n = 5;
            return n;
        }
        else if(myBoard.getMark(6) == 'X' && myBoard.getMark(7) == 'X' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // - X X
        // - X X
        // - X X
        else if(myBoard.getMark(1) == 'X' && myBoard.getMark(2) == 'X' && myBoard.getMark(0) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(4) == 'X' && myBoard.getMark(5) == 'X' && myBoard.getMark(3) == '-')
        {
            n = 3;
            return n;
        }
        else if(myBoard.getMark(7) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }

        // X - X
        // X - X
        // X - X
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(2) == 'X' && myBoard.getMark(1) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(3) == 'X' && myBoard.getMark(5) == 'X' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(6) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(7) == '-')
        {
            n = 7;
            return n;
        }

        // X X X
        // X X X
        // - - -
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(3) == 'X' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }
        else if(myBoard.getMark(1) == 'X' && myBoard.getMark(4) == 'X' && myBoard.getMark(7) == '-')
        {
            n = 7;
            return n;
        }
        else if(myBoard.getMark(2) == 'X' && myBoard.getMark(5) == 'X' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // - - -
        // X X X
        // X X X
        else if(myBoard.getMark(3) == 'X' && myBoard.getMark(6) == 'X' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }
        else if(myBoard.getMark(4) == 'X' && myBoard.getMark(7) == 'X' && myBoard.getMark(1) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(5) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }

        // X X X
        // - - -
        // X X X
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(6) == 'X' && myBoard.getMark(3) == '-')
        {
            n = 3;
            return n;
        }
        else if(myBoard.getMark(1) == 'X' && myBoard.getMark(7) == 'X' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(2) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(5) == '-')
        {
            n = 5;
            return n;
        }

        // - X X
        // X - X
        // X X -
        else if(myBoard.getMark(4) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(4) == 'X' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // X X -
        // X - X
        // - X X
        else if(myBoard.getMark(4) == 'X' && myBoard.getMark(6) == 'X' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }
        else if(myBoard.getMark(6) == 'X' && myBoard.getMark(2) == 'X' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(6) == 'X' && myBoard.getMark(4) == 'X' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }

        // O O -
        // O O -
        // O O -
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(1) == 'O' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }
        else if(myBoard.getMark(3) == 'O' && myBoard.getMark(4) == 'O' && myBoard.getMark(5) == '-')
        {
            n = 5;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(7) == 'O' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // - O O
        // - O O
        // - O O
        else if(myBoard.getMark(1) == 'O' && myBoard.getMark(2) == 'O' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }
        else if(myBoard.getMark(4) == 'O' && myBoard.getMark(5) == 'O' && myBoard.getMark(3) == '-')
        {
            n = 3;
            return n;
        }
        else if(myBoard.getMark(7) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }

        // O - O
        // O - O
        // O - O
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(2) == 'O' && myBoard.getMark(1) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(3) == 'O' && myBoard.getMark(5) == 'O' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(7) == '-')
        {
            n = 7;
            return n;
        }

        // O O O
        // O O O
        // - - -
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(3) == 'O' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }
        else if(myBoard.getMark(1) == 'O' && myBoard.getMark(4) == 'O' && myBoard.getMark(7) == '-')
        {
            n = 7;
            return n;
        }
        else if(myBoard.getMark(2) == 'O' && myBoard.getMark(5) == 'O' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // O O O
        // - - -
        // O O O
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(6) == 'O' && myBoard.getMark(3) == '-')
        {
            n = 3;
            return n;
        }
        else if(myBoard.getMark(1) == 'O' && myBoard.getMark(7) == 'O' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(2) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(5) == '-')
        {
            n = 5;
            return n;
        }

        // - - -
        // O O O
        // O O O
        else if(myBoard.getMark(3) == 'O' && myBoard.getMark(6) == 'O' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }
        else if(myBoard.getMark(4) == 'O' && myBoard.getMark(7) == 'O' && myBoard.getMark(1) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(5) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }

        // - O O
        // O - O
        // O O -
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(4) == 'O' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(4) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }

        // O O -
        // O - O
        // - O O
        else if(myBoard.getMark(4) == 'O' && myBoard.getMark(2) == 'O' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(2) == 'O' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(4) == 'O' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }


        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(4) == 'X' && myBoard.getMark(8) == 'O')
        {
            n = 5;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(4) == 'X' && myBoard.getMark(2) == 'O')
        {
            n = 3;
            return n;
        }

        else if(myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }

        else
        {
            Random generator = new Random();
            boolean generate = true;
            while (generate)
            {
                n = generator.nextInt(9); // gefur tölu, frá 0 til 8
                if ( myBoard.getMark(n) == '-' )
                    generate = false; //stop generation
            }
            Logger.info("Generated number: " + n);
            return n;
        }
    }

}
